import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import java.util.ArrayList;

/**
 * represents a selection phase in a MULE game
 * 
 * @author Sherman
 *
 */
@SuppressWarnings("serial")
public class SelectionPhase extends JPanel{

	private boolean selectionEnded;	//if the selection has ended	
	private int passes; 			//used to end phase when each player passes in a round
	private int clickCount; 		//used for determining player turn and round
	private JPanel panel;			//the panel everything is placed inside of
	private JPanel mapPanel;		//the map panel
	private int numPlayers;			//the total number of players
	private ArrayList<Player> players;	//an array list of players
	private Map map;				//the map that's being used for selection

	/**
	 * creates an instance of a selection phase
	 */
	public SelectionPhase(){
		passes = 0;
		clickCount = 0;
		selectionEnded = false;
		

	}

	/**
	 * Generates a selection panel based off of players and map
	 * @param nPlayers the number of players
	 * @param ps an array list of the players
	 * @param m the map
	 * @return a selection phase panel
	 */
	public JPanel generateSelectionPanel(int nPlayers, ArrayList<Player> ps, Map m) {
		
		
		numPlayers = nPlayers;
		players = ps;
		map = m;
		
		final JLabel status = new JLabel("");

		panel = new JPanel();
		panel.setLayout(new BorderLayout());
		
		mapPanel = new JPanel();
		mapPanel.setLayout(new GridLayout(5, 9));
		status.setText("players will have to start paying on turn " + (numPlayers * 2 + 1));
		for (int i = 0; i < 45; i++) {

			//just a final copy of i to use in inner class
			final int j = i;

			final JPanel temp = new JPanel();

			//take button that goes in each property
			final JButton takeButton = new JButton("take");

			//pass button that goes in town
			final JButton passButton = new JButton("pass");

			//uses clickCount and numPlayers to give a property to a player
			//also uses clickCount and numPlayers to decide if property will cost money
			takeButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					if (e.getSource() == takeButton){

						//gets the player number for players[]
						int pNum = (clickCount % numPlayers);

						clickCount += 1;

						//player will get the property

						status.setText("giving " + players.get(pNum).getName()+ " the property");

						//add property to player list				
						players.get(pNum).addProperty(map.getProperty(j));
					
						//set property owner
						map.getProperty(j).setOwner(players.get(pNum));

						//remove the button so players can no longer pick this property
						temp.setVisible(false);

						//all players have went in this round
						//reset passes for next round
						if (clickCount % numPlayers == 0 && clickCount / numPlayers >= 2){
							passes = 0;
							status.setText("Gave player " + players.get(pNum).getName() + " the property. Starting new selection round");
							selectionEnded = true;
						}
					}
				}
			});

			passButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					if (e.getSource() == passButton){

						passes += 1;

						clickCount += 1;

						//gets the player number for players[]
						int pNum = (clickCount % numPlayers);

						status.setText(players.get(pNum).getName()+ " passed");

						if (passes == numPlayers){
							status.setText("PASS LIMIT MET; SELECTION PHASE ENDED");
							selectionEnded = true;

						}

						//start a new selection round and reset passes
						else if (clickCount % numPlayers == 0 && clickCount / numPlayers >= 2){
							status.setText(players.get(pNum).getName()+ " passed. Starting new round");
							passes = 0;
							selectionEnded = true;
						}
					}
				}
			});

			//set property colors
			temp.setSize(new Dimension(50, 50));
			if (map.getProperty(i).getTerrainNum() == 1) {
				temp.setBackground(GameModel.plainGreen);
			}
			else if (map.getProperty(i).getTerrainNum() > 1 && map.getProperty(i).getTerrainNum() <= 4) {	
				temp.setBackground(GameModel.mountainBrown);
			}
			else if (map.getProperty(i).getTerrainNum() == 5){
				temp.setBackground(GameModel.riverBlue);
			}
			else {
				temp.setBackground(GameModel.townOrange);
			}

			temp.add(new JLabel((map.getProperty(i)).getTerrainType()), BorderLayout.PAGE_START);

			if (i != 22 && map.getProperty(i).getOwner() == null){
				//take button for each property
				temp.add(takeButton);

			}

			else if (i == 22){
				//put the pass button in the town since it's empty anyway
				temp.add(passButton);

			}

			mapPanel.add(temp);
		}
		panel.add(status, BorderLayout.NORTH);
		panel.add(mapPanel);
		return panel;
	}

	/**
	 * checks if selection phase has ended
	 * @return whether or not selection phase has ended
	 */
	public boolean selectionEnded() {
		return selectionEnded;
	}	
}